package com.topisv.tms.web.workguaranteetask;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.topisv.tms.entity.JSONParam;
import com.topisv.tms.entity.QueryParamHolder;
import com.topisv.tms.entity.ShopSource;
import com.topisv.tms.entity.WorkGuaranteeTask;
import com.topisv.tms.entity.work.WorkOrder;
import com.topisv.tms.service.ShopSourceService;
import com.topisv.tms.service.WorkGuaranteeTaskService;
import com.topisv.tms.service.work.WorkOrderService;
import com.topisv.tms.web.base.BaseDataTableController;

@Controller
@RequestMapping(value="/v2/workGuaranteeTask")
public class WorkGuaranteeTaskController extends BaseDataTableController{
	private static final Logger logger = LoggerFactory.getLogger(WorkGuaranteeTaskController.class);
	
	@Autowired
	private WorkGuaranteeTaskService workGuaranteeTaskService;
	@Autowired
	private WorkOrderService workOrderService;
	@Autowired
	private ShopSourceService shopSourceService;
	
	@RequestMapping(value="workGuaranteeTaskList")
	public String list(Model model){
		fillSelectValue(model, null);
		//初始化商家店铺
		List<ShopSource> shopList=this.shopSourceService.findByGuaranteeStatus();
		model.addAttribute("shopList", shopList);
		return "work/guaranteePrint/guaranteePrintList";
	}
	
	@RequestMapping(value="page")
	public void page(@RequestBody JSONParam[] paramJson, HttpServletRequest request,HttpServletResponse response){
		try {
			// 客户端参数
			QueryParamHolder paramHolder = collectParam(paramJson);
			Map<String, Object> searchParams = paramHolder.getSearchParams();
			String fromTm = (String)searchParams.get("GTE_createTime");
			String toTm = (String)searchParams.get("LT_createTime");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if(!StringUtils.isBlank(fromTm)){
				searchParams.put("GTE_createTime",sdf.parse(fromTm));
			}
			if(!StringUtils.isBlank(toTm)){
				searchParams.put("LT_createTime",sdf.parse(toTm));
			};
			Page<WorkGuaranteeTask> page = workGuaranteeTaskService.loadWorkGuaranteeTask(paramHolder);
			// 封装查询结果
			response.getWriter().print(bulidJSON(page));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 保修单打印
	 * @param workNos
	 * @param number
	 * @param ordeno
	 * @param response
	 */
	@RequestMapping(value = "ajaxPrintGuarantee", method = RequestMethod.POST)
	public void ajaxPrintGuarantee(
			@RequestParam(value = "taskCode") String taskCode,
			@RequestParam(value = "shopCode") String shopCode,
			HttpServletResponse response) {
		PrintWriter out=null;
		JSONObject json=null;
		try {
				json=new JSONObject();
				out=response.getWriter();
				WorkGuaranteeTask guaranteeTask = workGuaranteeTaskService.findByTaskCode(taskCode);
				List<WorkOrder> workOrders = workOrderService.findByTaskCodes(taskCode);
				json.put("workOrders", workOrders);
				json.put("status","1");
				json.put("shopCode", guaranteeTask.getShopCode());
				out.print(json.toString());
		} catch (Exception e) {
			
			logger.error("打印加载错误", e);
		}
	}
	
	@RequestMapping(value = "updateGuarantee", method = RequestMethod.POST)
	public void updateGuarantee(@RequestParam(value = "taskCode") String taskCode,HttpServletResponse response){
		PrintWriter out=null;
		JSONObject json=null;
		try {
			logger.info("开始修改保修单状态==taskCode==="+taskCode);
			json=new JSONObject();
			out=response.getWriter();
			WorkGuaranteeTask workGuaranteeTask=this.workGuaranteeTaskService.findByTaskCode(taskCode);
			if(workGuaranteeTask==null){
				json.put("status", "0");
				out.print(json.toString());
				return;
			}
			this.workGuaranteeTaskService.updateGuaranteeStatus(workGuaranteeTask);
			json.put("status", "1");
			json.put("content", workGuaranteeTask);
			out.print(json.toString());
		} catch (Exception e) {
			
			json.put("status", "0");
			out.print(json.toString());
			logger.error("加载错误", e);
		}
		logger.info(json.toString());
	}
	/**
	 * 导向重新保修单打印页面
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "v2_toReprintPage/{taskCode}/{batchCode}", method = RequestMethod.GET)
	public String toReprintPage(@PathVariable("taskCode") String taskCode, @PathVariable("batchCode") String batchCode,Model model) {
		try {

			 Map<String, Object> param=new HashMap<String, Object>();
			 param.put("guaranteeCode", taskCode);
			 param.put("batchCode", batchCode);
			 List<WorkOrder> orderList=this.workOrderService.loadOrder(param);
			 model.addAttribute("orderList",orderList);
			 model.addAttribute("taskCode",taskCode);
		} catch (Exception e) {
			
			logger.error("error",e);
		}
		return "work/guaranteePrint/reGuaranteePrint";
	}	
	/**
	 * 重新保修单打印
	 * @param workNos
	 * @param number
	 * @param ordeno
	 * @param response
	 */
	@RequestMapping(value = "ajaxPrintguaranteeOrdenos", method = RequestMethod.POST)
	public void ajaxPrintguaranteeOrdenos(
			@RequestParam(value = "ordenos") String ordenos,
			HttpServletResponse response) {
		PrintWriter out=null;
		JSONObject json=null;
		try {
				json=new JSONObject();
				out=response.getWriter();
				List<WorkOrder> workOrder = workOrderService.findByordenos(ordenos);
				json.put("workOrders",workOrder);
				json.put("status","1");
				json.put("shopCode", workOrder.get(0).getOrder().getShopCode());
				out.print(json.toString());
		} catch (Exception e) {
			
			logger.error("打印加载错误", e);
		}
	}
	/**
	 * 根据商家编码获取商铺信息
	 * @param customerCode
	 * @param response
	 */
	@RequestMapping(value="ajaxShopsource")
	public void ajaxShopsource(@RequestParam(value="customerCode") String customerCode,HttpServletResponse response){
		JSONObject json=null;
		PrintWriter out=null;
		List<ShopSource> shopSourceList=null;
		try {
			out=response.getWriter();
			json=new JSONObject();
			
			shopSourceList=this.shopSourceService.findByGuaranteeStatus(customerCode);
			
			json.put("content", shopSourceList);
			json.put("status", "1");
			out.print(json.toString());
			log.debug("根据商家编码获取网点信息:"+json.toString());
		} catch (Exception e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
		}
	}
	
	
}
